<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::DateTime::Calculations</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::DateTime::Calculations</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Enables the use of time calculations within DateTime itself
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000724">advance</a>&nbsp;&nbsp;
      <a href="#M000725">ago</a>&nbsp;&nbsp;
      <a href="#M000731">at_beginning_of_day</a>&nbsp;&nbsp;
      <a href="#M000730">at_midnight</a>&nbsp;&nbsp;
      <a href="#M000728">beginning_of_day</a>&nbsp;&nbsp;
      <a href="#M000723">change</a>&nbsp;&nbsp;
      <a href="#M000732">end_of_day</a>&nbsp;&nbsp;
      <a href="#M000727">in</a>&nbsp;&nbsp;
      <a href="#M000729">midnight</a>&nbsp;&nbsp;
      <a href="#M000722">seconds_since_midnight</a>&nbsp;&nbsp;
      <a href="#M000726">since</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="Calculations/ClassMethods.html" class="link">ActiveSupport::CoreExtensions::DateTime::Calculations::ClassMethods</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000724" class="method-detail">
        <a name="M000724"></a>

        <div class="method-heading">
          <a href="#M000724" class="method-signature">
          <span class="method-name">advance</span><span class="method-args">(options)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Uses Date to provide precise Time calculations for years, months, and days.
The <tt>options</tt> parameter takes a hash with any of these keys: :years,
:months, :weeks, :days, :hours, :minutes, :seconds.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000724-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000724-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 42</span>
42:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">advance</span>(<span class="ruby-identifier">options</span>)
43:           <span class="ruby-identifier">d</span> = <span class="ruby-identifier">to_date</span>.<span class="ruby-identifier">advance</span>(<span class="ruby-identifier">options</span>)
44:           <span class="ruby-identifier">datetime_advanced_by_date</span> = <span class="ruby-identifier">change</span>(<span class="ruby-identifier">:year</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">year</span>, <span class="ruby-identifier">:month</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">month</span>, <span class="ruby-identifier">:day</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">day</span>)
45:           <span class="ruby-identifier">seconds_to_advance</span> = (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:seconds</span>] <span class="ruby-operator">||</span> <span class="ruby-value">0</span>) <span class="ruby-operator">+</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:minutes</span>] <span class="ruby-operator">||</span> <span class="ruby-value">0</span>) <span class="ruby-operator">*</span> <span class="ruby-value">60</span> <span class="ruby-operator">+</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:hours</span>] <span class="ruby-operator">||</span> <span class="ruby-value">0</span>) <span class="ruby-operator">*</span> <span class="ruby-value">3600</span>
46:           <span class="ruby-identifier">seconds_to_advance</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">datetime_advanced_by_date</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">datetime_advanced_by_date</span>.<span class="ruby-identifier">since</span>(<span class="ruby-identifier">seconds_to_advance</span>)
47:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000725" class="method-detail">
        <a name="M000725"></a>

        <div class="method-heading">
          <a href="#M000725" class="method-signature">
          <span class="method-name">ago</span><span class="method-args">(seconds)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a new DateTime representing the time a number of seconds <a
href="Calculations.html#M000725">ago</a> Do not use this method <a
href="Calculations.html#M000727">in</a> combination with x.months, use
months_ago instead!
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000725-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000725-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 51</span>
51:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">ago</span>(<span class="ruby-identifier">seconds</span>)
52:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">since</span>(<span class="ruby-operator">-</span><span class="ruby-identifier">seconds</span>)
53:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000731" class="method-detail">
        <a name="M000731"></a>

        <div class="method-heading">
          <span class="method-name">at_beginning_of_day</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Calculations.html#M000728">beginning_of_day</a>
</p>
        </div>
      </div>

      <div id="method-M000730" class="method-detail">
        <a name="M000730"></a>

        <div class="method-heading">
          <span class="method-name">at_midnight</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Calculations.html#M000728">beginning_of_day</a>
</p>
        </div>
      </div>

      <div id="method-M000728" class="method-detail">
        <a name="M000728"></a>

        <div class="method-heading">
          <a href="#M000728" class="method-signature">
          <span class="method-name">beginning_of_day</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a new DateTime representing the start of the day (0:00)
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000728-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000728-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 63</span>
63:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">beginning_of_day</span>
64:           <span class="ruby-identifier">change</span>(<span class="ruby-identifier">:hour</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>)
65:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000723" class="method-detail">
        <a name="M000723"></a>

        <div class="method-heading">
          <a href="#M000723" class="method-signature">
          <span class="method-name">change</span><span class="method-args">(options)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a new DateTime where one or more of the elements have been changed
according to the <tt>options</tt> parameter. The time options (hour,
minute, sec) reset cascadingly, so if only the hour is passed, then minute
and sec is set to 0. If the hour and minute is passed, then sec is set to
0.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000723-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000723-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 27</span>
27:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">change</span>(<span class="ruby-identifier">options</span>)
28:           <span class="ruby-operator">::</span><span class="ruby-constant">DateTime</span>.<span class="ruby-identifier">civil</span>(
29:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:year</span>]  <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">year</span>,
30:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:month</span>] <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">month</span>,
31:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:day</span>]   <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">day</span>,
32:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:hour</span>]  <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">hour</span>,
33:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:min</span>]   <span class="ruby-operator">||</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:hour</span>] <span class="ruby-operator">?</span> <span class="ruby-value">0</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">min</span>),
34:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:sec</span>]   <span class="ruby-operator">||</span> ((<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:hour</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:min</span>]) <span class="ruby-operator">?</span> <span class="ruby-value">0</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">sec</span>),
35:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:offset</span>]  <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">offset</span>,
36:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:start</span>]  <span class="ruby-operator">||</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">start</span>
37:           )
38:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000732" class="method-detail">
        <a name="M000732"></a>

        <div class="method-heading">
          <a href="#M000732" class="method-signature">
          <span class="method-name">end_of_day</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a new DateTime representing the end of the day (23:59:59)
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000732-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000732-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 71</span>
71:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">end_of_day</span>
72:           <span class="ruby-identifier">change</span>(<span class="ruby-identifier">:hour</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">23</span>, <span class="ruby-identifier">:min</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">59</span>, <span class="ruby-identifier">:sec</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">59</span>)
73:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000727" class="method-detail">
        <a name="M000727"></a>

        <div class="method-heading">
          <span class="method-name">in</span><span class="method-args">(seconds)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Calculations.html#M000726">since</a>
</p>
        </div>
      </div>

      <div id="method-M000729" class="method-detail">
        <a name="M000729"></a>

        <div class="method-heading">
          <span class="method-name">midnight</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Calculations.html#M000728">beginning_of_day</a>
</p>
        </div>
      </div>

      <div id="method-M000722" class="method-detail">
        <a name="M000722"></a>

        <div class="method-heading">
          <a href="#M000722" class="method-signature">
          <span class="method-name">seconds_since_midnight</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Seconds <a href="Calculations.html#M000726">since</a> <a
href="Calculations.html#M000729">midnight</a>: DateTime.now.<a
href="Calculations.html#M000722">seconds_since_midnight</a>
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000722-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000722-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 20</span>
20:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">seconds_since_midnight</span>
21:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">sec</span> <span class="ruby-operator">+</span> (<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">min</span> <span class="ruby-operator">*</span> <span class="ruby-value">60</span>) <span class="ruby-operator">+</span> (<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">hour</span> <span class="ruby-operator">*</span> <span class="ruby-value">3600</span>)
22:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000726" class="method-detail">
        <a name="M000726"></a>

        <div class="method-heading">
          <a href="#M000726" class="method-signature">
          <span class="method-name">since</span><span class="method-args">(seconds)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a new DateTime representing the time a number of seconds <a
href="Calculations.html#M000726">since</a> the instance time Do not use
this method <a href="Calculations.html#M000727">in</a> combination with
x.months, use months_since instead!
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000726-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000726-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 57</span>
57:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">since</span>(<span class="ruby-identifier">seconds</span>)
58:           <span class="ruby-keyword kw">self</span> <span class="ruby-operator">+</span> <span class="ruby-constant">Rational</span>(<span class="ruby-identifier">seconds</span>.<span class="ruby-identifier">round</span>, <span class="ruby-value">86400</span>)
59:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>